/*
 * @Author: Siogo
 * @Date: 2025-11-04 17:45:00
 * @LastEditors: Siogo 你的邮箱
 * @LastEditTime: 2025-11-05 11:42:20
 * @FilePath: /frontend/src/api/tb/platformLog.ts
 * @Description: 平台日志管理API
 */
import { Page } from '../model/baseModel';
import { defHttp } from '/@/utils/http/axios';

/**
 * 日志类型枚举
 */
export enum LogType {
  EXCEPTION_LOG = 'EXCEPTION_LOG',
  BUSINESS_LOG = 'BUSINESS_LOG',
  AUDIT_LOG = 'AUDIT_LOG',
  OPERATION_LOG = 'OPERATION_LOG',
}

/**
 * 日志级别枚举
 */
export enum LogLevel {
  CRITICAL = 'CRITICAL',
  WARNING = 'WARNING',
  INFO = 'INFO',
}

/**
 * 实体ID接口
 */
export interface EntityId {
  id: string;
  entityType: string;
}

/**
 * 平台日志对象接口
 */
export interface PlatformLog {
  id: EntityId;
  createdTime: number;
  tenantId?: EntityId;
  userId?: EntityId;
  userName?: string;
  logType: LogType;
  logLevel?: LogLevel;
  businessId?: string;
  operationContent: string;
  operationDetails?: string;
  operationParams?: string;
  statusTransition?: string;
  exceptionInfo?: string;
  occurTime: number;
  ipAddress?: string;
  status?: string;
  successful?: boolean;
  sensitiveOperation?: boolean;
}

/**
 * 平台日志查询参数接口
 */
export interface PlatformLogQueryParam {
  page: number;
  pageSize: number;
  userId?: string;
  logType?: LogType;
  logLevel?: LogLevel;
  startTime?: number;
  endTime?: number;
  textSearch?: string;
  sortProperty?: string;
  sortOrder?: string;
}

/**
 * 导出参数接口
 */
export interface PlatformLogExportParam {
  userId?: string;
  logType?: LogType;
  logLevel?: LogLevel;
  startTime?: number;
  endTime?: number;
  textSearch?: string;
  format: 'CSV' | 'EXCEL';
}

/**
 * 获取平台日志列表
 */
export function getPlatformLogList(params: PlatformLogQueryParam) {
  return defHttp.get<Page<PlatformLog>>({
    url: '/api/platform/logs',
    params,
  });
}

/**
 * 根据ID获取平台日志详情
 */
export function getPlatformLogById(logId: string) {
  return defHttp.get<PlatformLog>({
    url: `/api/platform/log/${logId}`,
  });
}

/**
 * 导出平台日志
 */
export function exportPlatformLogs(params: PlatformLogExportParam) {
  return defHttp.postJson<Blob>({
    url: '/api/platform/logs/export',
    data: params,
    responseType: 'blob',
  });
}
